package com.neusoft.hifly.rpc.email.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.rpc.email.dao.IEmailDao;
import com.neusoft.hifly.rpc.email.domain.MailDTO;
import com.neusoft.hifly.rpc.email.service.IEmailService;

/**
 * Title: 邮件服务
 * <p>
 * Description: 邮件 Dao
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service("emailService")
public class EmailServiceImpl implements IEmailService {

	@Autowired(required = false)
	private IEmailDao emailDao;

	/**
	 * 发布订阅模式
	 *
	 * @param pushObj
	 *            对象
	 */
	@EventListener
	private void handlerEmailEvent(final MailDTO pushObj) {
		sendMail(pushObj);
	}

	/**
	 * 发送邮件
	 *
	 * @param obj
	 *            邮件对象
	 * @return 返回 如果发送成功返回true 否则返回false
	 */
	@Override
	public boolean sendMail(final MailDTO obj) {
		if (obj == null || StringUtils.isEmpty(obj.getEmail()) || StringUtils.isEmpty(obj.getContent())) {
			return false;
		}
		final ApiResponse<Object> result = emailDao.sendMail(obj);

		if (result.getCode() == ApiCode.ERROR.getCode()) {
			Log.error(result.getMessage());
			return false;
		}

		return true;
	}
}
